<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('股东类型')" prop="shareholderType">
            <dictSelect
              v-model="form.shareholderType"
              :clearable="true"
              dictionary="internal_shareholder_type"
              @change="switchType"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('股东名称')" prop="shareholderName">
            <template v-if="form.shareholderType === 'NBJG' || form.shareholderType === 'JJGL' ">
              <el-select v-model="form.shareholderName" filterable placeholder="请选择">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </template>
            <template v-else>
              <el-input v-model="form.shareholderName" :placeholder="$t('股东名称')" maxlength="50" show-word-limit />
            </template>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('持股比例')" prop="ratio">
            <Ratio v-model="form.ratio" :min="0" :max="9999" />

          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('备注')" prop="remark">
            <el-input
              v-model="form.remark"
              type="textarea"
              :rows="2"
              :placeholder="$t('备注')"
              maxlength="1000"
              show-word-limit
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="system:shareholder" />
    </div>
  </div>
</template>

<script>
    import { saveInternalShareholder, getInternalShareholder, getShareholderOrFundByType, unique } from '@/api/fund/shareholder/internalShareholder'
    export default {
        data() {
          // 唯一校验
          var checkUnique = (rule, value, callback) => {
            if (value === '') {
             return callback(new Error('必填信息'))
            } else {
                unique(value, this.form.id, this.fundId).then(res => {
                   if (res.data === 1) {
                       callback(new Error('已存在该数据'))
                     } else {
                        callback()
                     }
                })
            }
          }
            return {
                // 表单参数
                form: { },
                // 遮罩层
                loading: false,
                // 表单校验
                rules: {
                   shareholderType: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                  ], shareholderName: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' },
                      { validator: checkUnique, trigger: ['blur', 'change'] }
                  ],
                   ratio: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                  ]
                },
                fundId: this.$route.query.id,
                options: []
            }
        },
        watch: {
          'form.shareholderType': {
            handler(newVal, oldVal) {
              if ((newVal && oldVal) && (newVal !== oldVal)) {
                this.form.shareholderName = ''
              }
            },
            deep: true,
            immediate: true
          }
        },
        created() {
        },
        methods: {
            // 初始化数据
           async  init(data) {
                this.loading = true
                if (data != null && data.id !== undefined) {
                    getInternalShareholder(data.id).then(response => {
                        this.form = response.data
                        this.loading = false
                    }).catch(() => {
                      this.loading = false
                    })
                } else {
                    this.reset()
                    this.loading = false
                }
            },
            // 表单重置
            reset() {
                this.form = {
                    id: '',
                    internalId: this.$route.query.id,
                    shareholderType: undefined,
                    shareholderName: undefined,
                    ratio: 0,
                    remark: undefined
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                if (this.$parent.$parent.formOpen !== undefined) {
                  this.$parent.$parent.formOpen = false
                }
                // 刷新list页面
                if (this.$parent.$parent.getList !== undefined) {
                  this.$parent.$parent.getList()
                }
            },
            /** 提交按钮 */
            submitForm() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        this.loading = true
                         this.internalId = this.fundId
                        saveInternalShareholder(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                                this.closeForm()
                            } else {
                                this.msgError(response.msg)
                            }
                            this.loading = false
                        })
                    } else {
                       this.locationError(document)
                    }
                })
           },
            switchType(val) {
              // this.form.shareholderName = undefined
              if (val === 'NBJG') { // 内部机构:选内部机构（排除自己）
                // 1. 查询机构数据
                getShareholderOrFundByType(val).then(res => {
                  this.options = res.data
                  // 过滤自己
                  const newArr = res.data.filter(item => {
                    return item.value !== this.fundId
                  })
                  this.options = newArr
                })
              } else if (val === 'JJGL') { // 基金管理:选基金
                  getShareholderOrFundByType(val).then(res => {
                    this.options = res.data
                  })
              } else if (val === 'QT') { // 其他：手动输入

              }
           }

        }
    }
</script>
